
* 1) Baseline
eststo clear
use tempfirst, clear
global depvar = "theil"
global depvar_d1 = "theil_d1"
global nbnc8iso2 " "
global nbnc8iso2_d1 " "
egen isiciso = group(isic iso2_d)
*
gen cond = abs((nbnc8iso2isic-L.nbnc8iso2isic)/L.nbnc8iso2isic)
gen byte cond5 = cond==0
*
global clustvar "firmiso"
eststo : reghdfe $depvar ltradeshock3  lgdpcapshock3 $nbnc8iso2  yd* ,  cluster($clustvar) a(firmiso)
global clustvar "isiciso"
eststo : reghdfe $depvar ltradeshock3i lgdpcapshock3 $nbnc8iso2  yd* ,  cluster($clustvar) a(firmiso)
*
global clustvar "firmiso"
eststo : reg $depvar_d1  ltradeshock6  lgdpcapshock6 $nbnc8iso2_d1  yd* , robust cluster($clustvar)
global clustvar "isiciso"
eststo : reg $depvar_d1  ltradeshock6i lgdpcapshock6 $nbnc8iso2_d1  yd* , robust cluster($clustvar)
*
global clustvar "firmiso"
eststo : reghdfe $depvar_d1  ltradeshock6  lgdpcapshock6 $nbnc8iso2_d1 yd* ,  cluster($clustvar) a(firmiso)
global clustvar "isiciso"
eststo : reghdfe $depvar_d1  ltradeshock6i lgdpcapshock6 $nbnc8iso2_d1 yd* ,  cluster($clustvar) a(firmiso)
*
log using $DIRLOG/Replication/tab-firstststagetrue_rev.txt, text replace
set linesize 250
esttab , keep(ltradeshock* )  b(%4.3f) se(%4.3f) r2 starlevels({$^c$} 0.1 {$^b$} 0.05 {$^a$} 0.01)  tex  title() label
esttab , keep(ltradeshock* ) compress r2 starlevels(c 0.1 b 0.05 a 0.01)  b(%4.3f) se(%4.3f)
*output the results in a tex file directly inputable in the text
esttab using $DIRLOG/Replication/tab-firstststagetrue_rev_input.tex, b(%4.3f) se(%4.3f)  starlevels({$^c$} 0.1 {$^b$} 0.05 {$^a$} 0.01)    ///
nocons drop(yd* _cons lgdpcapshock* )  label tex substitute(\_ _) fragment replace nodepvars nonumbers nomtitles nogaps nolines eqlabels(none)
eststo clear
log close

* 2) With leads and lags (also generates output for the graph)
eststo clear
use tempfirst, clear
global depvar = "theil"
global depvar_d1 = "theil_d1"
global nbnc8iso2 " "
global nbnc8iso2_d1 " "
egen isiciso = group(isic iso2_d)
*
gen cond = abs((nbnc8iso2isic-L.nbnc8iso2isic)/L.nbnc8iso2isic)
gen byte cond5 = cond==0
*
global clustvar "firmiso"
eststo : reghdfe $depvar ltradeshock3_l2 ltradeshock3_l1 ltradeshock3 ltradeshock3_f1 ltradeshock3_f2  lgdpcapshock3 $nbnc8iso2  yd* ,  cluster($clustvar) a(firmiso)
regsave using temp_leadlag_tab3_FE,replace
global clustvar "isiciso"
eststo : reghdfe $depvar ltradeshock3i_l2 ltradeshock3i_l1 ltradeshock3i ltradeshock3i_f1 ltradeshock3i_f2 lgdpcapshock3 $nbnc8iso2  yd* ,  cluster($clustvar) a(firmiso)
regsave using temp_leadlag_tab3_FEi,replace
*
global clustvar "firmiso"
eststo : reg $depvar_d1  ltradeshock6_l2 ltradeshock6_l1 ltradeshock6 ltradeshock6_f1 ltradeshock6_f2   lgdpcapshock6 $nbnc8iso2_d1  yd* , robust cluster($clustvar)
regsave using temp_leadlag_tab3_FD,replace
global clustvar "isiciso"
eststo : reg $depvar_d1  ltradeshock6i_l2 ltradeshock6i_l1 ltradeshock6i ltradeshock6i_f1 ltradeshock6i_f2 lgdpcapshock6 $nbnc8iso2_d1  yd* , robust cluster($clustvar)
regsave using temp_leadlag_tab3_FDi,replace
*
global clustvar "firmiso"
eststo : reghdfe $depvar_d1  ltradeshock6_l2 ltradeshock6_l1 ltradeshock6 ltradeshock6_f1 ltradeshock6_f2   lgdpcapshock6 $nbnc8iso2_d1 yd* ,  cluster($clustvar) a(firmiso)
regsave using temp_leadlag_tab3_FDFE,replace
global clustvar "isiciso"
eststo : reghdfe $depvar_d1  ltradeshock6i_l2 ltradeshock6i_l1 ltradeshock6i ltradeshock6i_f1 ltradeshock6i_f2 lgdpcapshock6 $nbnc8iso2_d1 yd* ,  cluster($clustvar) a(firmiso)
regsave using temp_leadlag_tab3_FDFEi,replace
*
log using $DIRLOG/Replication/tab-firstststagetrue_rev_robleadlag.txt, text replace
set linesize 250
esttab , keep(ltradeshock* )  b(%4.3f) se(%4.3f) r2 starlevels({$^c$} 0.1 {$^b$} 0.05 {$^a$} 0.01)  tex  title() label
esttab , keep(ltradeshock* ) compress r2 starlevels(c 0.1 b 0.05 a 0.01)  b(%4.3f) se(%4.3f)
*output the results in a tex file directly inputable in the text
esttab using $DIRLOG/Replication/tab-firstststagetrue_rev_robleadlag_input.tex, b(%4.3f) se(%4.3f)  starlevels({$^c$} 0.1 {$^b$} 0.05 {$^a$} 0.01)    ///
nocons keep(ltradeshock* )  label tex substitute(\_ _) fragment replace nodepvars nonumbers nomtitles nogaps nolines eqlabels(none)
eststo clear
log close



* 3) old version: needed to create the aggregated theil indices
use tempfirst, clear
global depvar = "theil"
global depvar_d1 = "theil_d1"
global nbnc8iso2 " "
global nbnc8iso2_d1 " "
global clustvar "iso2_d"
*
gen cond = abs((nbnc8iso2isic-L.nbnc8iso2isic)/L.nbnc8iso2isic)
gen byte cond5 = cond==0
*
eststo f_fe: xtreg $depvar lgdpshock3 ltradeshock3 ltradeshock3i  $nbnc8iso2  yd* , robust cluster($clustvar) fe i(firmiso)
predict theil_fe , xbu
gen entrop_fe = lnbnc8iso2 - theil_fe
gen samplefe = e(sample)
*
eststo f_fd: reg $depvar_d1 lgdpshock6 ltradeshock6 ltradeshock6i  $nbnc8iso2_d1  yd* , robust cluster($clustvar)
predict theil_fd if e(sample)==1
gen entrop_fd = lnbnc8iso2_d1 - theil_fd
gen samplefd = e(sample)
*
eststo f_fefd: xtreg $depvar_d1 lgdpshock6 ltradeshock6 ltradeshock6i  $nbnc8iso2_d1 yd* , robust cluster($clustvar) fe i(firmiso)
predict theil_fefd , xbu
gen entrop_fefd = lnbnc8iso2_d1 - theil_fefd
*
*now with constant delta theil
global depvar_d1 = "theil_dconst"
*
eststo f_fd_dconst: reg $depvar_d1 lgdpshock6 ltradeshock6 ltradeshock6i    yd* , robust cluster($clustvar)
predict theil_fdc if e(sample)==1
gen entrop_fdc =  - theil_fdc
gen samplefdc = e(sample)
*
eststo f_fefd_dconst: xtreg $depvar_d1 lgdpshock6 ltradeshock6 ltradeshock6i  yd* , robust cluster($clustvar) fe i(firmiso)
predict theil_fefdc , xbu
gen entrop_fefdc =  - theil_fefdc
*
log using $DIRLOG/Replication/tab-firstststagetrue.txt, text replace
set linesize 250
esttab f_fe f_fd f_fefd, drop(yd*) b(%4.3f) se(%4.3f) r2 starlevels({$^c$} 0.1 {$^b$} 0.05 {$^a$} 0.01)  tex  title() label
esttab f_fe f_fd f_fefd, drop(yd*) compress r2 starlevels(c 0.1 b 0.05 a 0.01)  b(%4.3f) se(%4.3f)
eststo clear
log close
*
**************************
*Now do the theil predict*
**************************
*
tabstat theil lgdpshock3 ltradeshock3 ltradeshock3i  if samplefe == 1  ///
, s(mean p50 p75 sd count)  format(%9.3g)  columns(statistics)  varwidth(20)

tabstat theil_d1 theil_dconst lgdpshock6 ltradeshock6 ltradeshock6i   if samplefd == 1 ///
, s(mean p50 p75 sd count)  format(%9.3g)  columns(statistics)  varwidth(20)
*
drop samplefe samplefd
tsset firmiso year
sort firmiso year
*
*predicted ones:
gen entrop_few = shrexp1st*entrop_fe
gen entrop_fdw = shrexplag*entrop_fd
gen entrop_fefdw = shrexplag*entrop_fefd
gen entrop_fdcw = shrexplag*entrop_fdc
gen entrop_fefdcw = shrexplag*entrop_fefdc
*
*measured one (NOT lagged, in order to verify aggregation property):
gen entropagg_w = shrexpcur*entrop
gen theil0agg_w = shrexpcur*theil0
gen theilagg_w = shrexpcur*theil
*
sort firmid year
save temp00,replace
*
qui bysort firmid year iso2_d : keep if _n == 1
gen obs = 1
collapse (sum) nbiso2 = obs, by(firmid year)
label var nbiso2 "nb destinations by firm/year"
sort firmid year
save temp000,replace
use temp00, clear
merge  firmid year using temp000
tab _merge
*perfect
drop _merge
erase temp00.dta
erase temp000.dta
*
collapse (sum) entrop_few entrop_fdw entrop_fefdw entrop_fdcw entrop_fefdcw entropagg_w theil0agg_w theilagg_w ///
(count) missentropagg_w = entropagg_w misstheil0agg_w = theil0agg_w misstheilagg_w = theilagg_w ///
missentrop_few = entrop_few ///
missentrop_fdw = entrop_fdw ///
missentrop_fefdw = entrop_fefdw ///
missentrop_fdcw = entrop_fdcw ///
missentrop_fefdcw = entrop_fefdcw ///
(min) nbnc8 nbiso2, by(firmid year)
*
gen lnbnc8 = ln(nbnc8)
tsset firmid year
*
gen theil_few = lnbnc8 - entrop_few
gen theil_fdw = D1.lnbnc8 - entrop_fdw
gen theil_fefdw = D1.lnbnc8 - entrop_fefdw
gen theil_fdcw =  - entrop_fdcw
gen theil_fefdcw =  - entrop_fefdcw
*
gen theil1agg_w = lnbnc8 - entropagg_w
*
 foreach x  in _few _fdw _fefdw _fdcw _fefdcw agg_w  {
replace theil`x' = . if missentrop`x'==0
}
replace theil0agg_w = . if misstheil0agg_w==0
replace theilagg_w = . if misstheilagg_w==0
replace theil1agg_w = . if missentropagg_w==0
*
label var entropagg_w "Aggregation of Destination Skewness (Entropy)"
label var theilagg_w "Aggregation of Destination Skewness (Theil)"
label var theil1agg_w "Aggregation of Destination Skewness (Theil)"
label var theil1agg_w "Aggregation of Destination Skewness (Theil)"
drop lnbnc8
sort firmid year
*
save theilpredict,replace
*






